package 力扣日常刷题.木22二月.第08天214;

import 力扣日常刷题.TreeNode;

/**
 * @author 帅小伙
 * @date 2022/2/14
 * @description
 */
public class Demo56JZ二叉搜索树的最近公共祖先 {


    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        TreeNode ans = root;
        while (true) {
            if(ans.val > p.val && ans.val > q.val) ans = ans.left;
            else if(ans.val < p.val && ans.val < q.val) ans = ans.right;
            else break;
        }
        return ans;
    }

}
